﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using TA.Turbo;
using TA.JSON;
using TA.Turbo.Controls;
using TA.DotNetUtils;

public partial class Default : TPage
{
	static readonly IEnumerable<DataObjectWithProperties> _dataWOReflection;
	static readonly IEnumerable<SomeDataObject> _dataWReflection;

	static Default()
	{
		_dataWOReflection = FakeDataProvider.GetDataWithProperties(1000);
		_dataWReflection = FakeDataProvider.GetData(1000);

	}

	bool UseReflection
	{
		get
		{
			return (bool)this.ControlState["UseReflection"];
		}
		set
		{
			this.ControlState["UseReflection"] = value;
		}
	}
	protected override void OnInit(EventArgs e)
	{

		base.OnInit(e);

		this.RegisterControlState(this);
		this.UseReflection = false;

		this.Includes.Add(Script.ElementSearch);
	}
	protected override void OnLoad(EventArgs e)
	{
		base.OnLoad(e);
		if (!Page.IsPostBack)
		{
			this.Bind();
		}
	}

	private void Bind()
	{
		if (this.UseReflection)
		{
			rpt.Value.Entries = new ReflectionRepeaterEntries();
			rpt.Value.Entries.AddRange(_dataWReflection.Take(rpt.Pager.Size));
			rpt.Value.NumEntries = _dataWReflection.Count();
		}
		else
		{
			rpt.Value.Entries = new PCRepeaterEntries();
			rpt.Value.Entries.AddRange(_dataWOReflection.Take(rpt.Pager.Size));
			rpt.Value.NumEntries = _dataWOReflection.Count();
		}
	}
	protected void btnLoadNoReflection_Click(object s, EventArgs e)
	{
		this.UseReflection = false;
		this.Bind();
	}
	protected void btnLoadReflection_Click(object s, EventArgs e)
	{
		this.UseReflection = true;
		this.Bind();
	}
	protected void rpt_LoadEntries(object s, RepeaterEntriesEventArgs e)
	{
		if (this.UseReflection)
		{
			e.Entries = new ReflectionRepeaterEntries();
			e.Entries.AddRange(_dataWReflection.Skip(e.FirstRow).Take(e.NumRows));
		}
		else
		{
			e.Entries = new PCRepeaterEntries();
			e.Entries.AddRange(_dataWOReflection.Skip(e.FirstRow).Take(e.NumRows));
		}
	}
	protected void rpt_CreateEntry(object s, RepeaterItemEventArgs e)
	{
		e.Entry = this.UseReflection ? new SomeDataObject() : new DataObjectWithProperties();
	}
	protected void rpt_ItemUpdated(object s, RepeaterItemEventArgs e)
	{
	}
}
